package com.dofast.bt.mvp.view.activity.basket;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dofast.bt.R;
import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.bean.AddressBean;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.mvp.presenter.basket.EditAddressPresenter;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/13.
 */

public class EditAddressActivity extends BaseMvpActivity<EditAddressPresenter, EditAddressView> implements EditAddressView, AdapterView.OnItemClickListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.lv_address)
    ListView lvAddress;
    private IntentFilter filter;
    private MyBroadercast myBroadercast;
    public static final String ADD_ADDRESS = EditAddressActivity.class.getSimpleName();
    public static final String ADDRESS = "address";

    @Override
    protected int getResourcesId() {
        return R.layout.activity_edit_address;
    }

    @Override
    protected void initView() {
        initTitle();
        presenter.initData();
        initBrodercast();
    }

    //初始化广播
    private void initBrodercast() {
        filter = new IntentFilter();
        filter.addAction(ADD_ADDRESS);
        myBroadercast = new MyBroadercast();
        registerReceiver(myBroadercast, filter);
    }

    @Override
    protected void addOnClickListener() {
        lvAddress.setOnItemClickListener(this);
    }

    @Override
    public Loader<EditAddressPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<EditAddressPresenter>() {
            @Override
            public EditAddressPresenter create() {
                return new EditAddressPresenter();
            }
        });
    }

    @Override
    public void initTitle() {
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("编辑地址");
        tvEdit.setText("新增");
    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    public static void gotoEditAddressActivity(Context context) {
        Intent intent = new Intent(context, EditAddressActivity.class);
        context.startActivity(intent);
    }

    @Override
    public List<GoodsBean> getGoods() {
        return (List<GoodsBean>) getIntent().getSerializableExtra("goods");
    }

    @Override
    public void gotoPaymentAcitity(AddressBean addressBean, List<GoodsBean> goodsBeen) {
        //界面跳转
//        PaymentActivity.gotoPaymentActivity(EditAddressActivity.this, addressBean, goodsBeen);
    }

    @Override
    public void selectAddress(AddressBean addressBean) {
        Intent intent = new Intent(SubmitOderActivity.SELECT_ADDRESS);
        intent.putExtra(SubmitOderActivity.ADDRESS, addressBean);
        sendBroadcast(intent);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_edit, R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_edit:
                // TODO: 2016/12/13   新增逻辑处理
                AddressActivity.gotoAddress(EditAddressActivity.this);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void initAdapter(Adapter adapter) {
        lvAddress.setAdapter((ListAdapter) adapter);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        presenter.selectAddress(position);
    }

    public class MyBroadercast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            AddressBean addressBean = (AddressBean) intent.getSerializableExtra(ADDRESS);
            presenter.refresh(addressBean);
        }
    }


}
